<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2017/8/28
 * Time: 11:55
 */

namespace app\operation\controller;

use app\common\model\ContractEquipment;
use think\Cache;
use think\Controller;
use app\common\model\Contract;
use app\common\model\Customer;
use app\common\model\Equipments;
use app\common\model\Parts;
use app\common\model\Engineers;
use app\common\model\Configuration;

/*
 *  合同控制器
 *  URL访问区分大小写时：ContractInfo
 *  URL访问不区分大小写时：Contract_Info（或者contract_info）
 */

class Contractinfo extends Base
{

    protected $Contract;

    public function __construct()
    {
        parent::__construct();
        //实例化合同model
        $this->Contract = new Contract('operation_user','operation_log');
    }

    /**
     * 合同列表
     * @author ligang
     * @Request array    $request     GET   搜索条件(一维数组)
     * @return array
     */
    public function index()
    {
        Cache::clear();
        //处理条件
        $request = input('');
        $where = array();
        $search = array(
            'contact_person'=>'',
            'contract_no'=>'',
            'id_card'=>''
        );
        foreach ($request as $key => $value){
            $search[$key] = $value;
            if (empty($value)){
                continue;
            }
            if ($key == 'contact_person'){
                $where['username'] = ['like','%'.$value.'%'];
                continue;
            }
            if ($key == 'operation_id'){
                $where['rq_contract.'.$key] = $value;
                continue;
            }
            if ($key == 'administrative_id'){
                $where['rq_contract.'.$key] = $value;
                continue;
            }
            $where[$key] = $value;
        }
        //获取数据
        $data = $this->Contract->selectPage($where);
        //获取运营中心
        $operation = $this->Contract->selectOperationInfo();
        //获取行政中心
        $administrative = $this->Contract->selectAdministrativeInfo();
        //模板赋值
        $this->assign('Nowpage', $data['page']['currentPage']); //当前页
        $this->assign('allpage', $data['page']['lastPage']); //总页数
        $this->assign('count', $data['page']['count']);//总条数
        $this->assign('search',$search);
        $this->assign('operation',$operation);
        $this->assign('administrative',$administrative);
        $this->assign('data',$data['data']);
        if(input('get.page')){
            return json($data);
        }
        //渲染模板
        return $this->fetch("/contract/agreement");
    }

    /**
     * 合同详情
     * @author ligang
     * @Request contract_id             int    GET   合同ID
     * @return array
     */
    public function details()
    {
        Cache::clear();
        //检验get参数
        if (input('?get.contract_id')){
            //获取参数并强制转换为int型
            $contract_id = input('get.contract_id/d');
        }else{
            return json(array('code'=>0,'msg'=>'缺少必要参数'));
        }

        $join = [
            ['rq_customer','rq_contract.user_id = rq_customer.user_id'],
            ['rq_contract_remark','rq_contract_remark.contract_no = rq_contract.contract_no','LEFT']
        ];
        $field = '
            rq_customer.username,
            rq_customer.employee_id,
            rq_customer.telphone,
            rq_customer.add_time,
            rq_customer.account_status,
            rq_customer.remark,
            rq_customer.user_address,
            rq_customer.province,
            rq_customer.city,
            rq_customer.area,
            rq_customer.birthday,
            rq_contract.contract_no,
            rq_contract.contract_id,
            rq_contract.introducer_id,
            rq_contract.applicant,
            rq_contract.receipt,
            rq_contract.water_pressure,
            rq_contract.contract_money,
            rq_contract.nature,
            rq_contract.original_parts,
            rq_contract.installed_time,
            rq_contract.recent_time,
            rq_contract.for_core_cycle,
            rq_contract.for_core_time,
            rq_contract.moving_machine_number,
            rq_contract.renew_date,
            rq_contract.exire_date,
            rq_contract.for_core_number,
            rq_contract.administrative_id,
            rq_contract.corporate_name,
            rq_contract.housing_property,
            rq_contract_remark.remark as contract_remark,
            rq_customer.is_entry
            ';
        //获取数据
        $data = $this->Contract->details(['contract_id'=>$contract_id],$join,$field);

        //转换客户状态
        foreach ($data as $key => $value){
            if ($key == 'account_status'){
                if ($value == 1){
                    $data[$key] = "正常";
                }else{
                    $data[$key] = "冻结";
                }
                continue;
            }
            if ($key == 'exire_date'){
                $data[$key] = date('Y-m-d',$value);
                continue;
            }
            if ($key == 'add_time'){
                $data[$key] = date('Y-m-d H:i:s',$value);
                continue;
            }
            if ($key == 'birthday'){
                $data[$key] = date('Y-m-d',$value);
                continue;
            }
            if ($key == 'installed_time'){
                $data[$key] = date('Y-m-d',$value);
                continue;
            }
            if ($key == 'recent_time'){
                $data[$key] = date('Y-m-d',$value);
                continue;
            }
            if ($key == 'for_core_time'){
                $data[$key] = date('Y-m-d',$value);
                continue;
            }
            if ($key == 'renew_date'){
                $data[$key] = date('Y-m-d',$value);
                continue;
            }
            if ($key == 'housing_property'){
                if ($value == 1){
                    $data[$key] = "公租房";
                }elseif ($value == 2){
                    $data[$key] = "廉租房";
                }elseif ($value == 3){
                    $data[$key] = "租赁";
                }else{
                    $data[$key] = "购买";
                }
                continue;
            }
            if ($key == 'nature'){
                //1.商用客户,2.家用客户,3外接包年,4.外接点餐,5.购买客户
                if ($value == 1){
                    $data[$key] = "商用客户";
                }elseif ($value == 2){
                    $data[$key] = "家用客户";
                }elseif ($value == 3){
                    $data[$key] = "外接包年";
                }elseif ($value == 4){
                    $data[$key] = "外接点餐";
                }else{
                    $data[$key] = "购买客户";
                }
                continue;
            }
        }
        //判断是否是员工
        if ($data['is_entry'] == 1){
            $data['is_entry'] = "是";
            $data['personnel_number'] = $data['employee_id'];
        }else{
            $data['is_entry'] = "否";
            $data['personnel_number'] = "";
        }

        //获取推荐人
        $Customer = new Customer();
        $data['introducer_id'] = $Customer->selectOne(['user_id'=>$data['introducer_id']])[0]['username'];
        $data['applicant'] = $Customer->selectOne(['user_id'=>$data['applicant']])[0]['username'];
        //获取设备台数
        $data['EquipmentNumber'] = $this->Contract->getEquipmentNumber(['contract_id'=>$contract_id,'state'=>1,'rq_contract_equipment.is_delete'=>0]);
        //获取设备记录
        $Equipment_join = [
            ['rq_equipments','rq_contract_equipment.equipments_id = rq_equipments.equipments_id'],
        ];
        //查询设备字段
        $Equipment_field = 'rq_equipments.equipments_name,
                            rq_contract_equipment.id,
                            rq_contract_equipment.equipments_id,
                            rq_contract_equipment.equipments_number,
                            rq_contract_equipment.equipments_address,
                            rq_contract_equipment.moving_machine_number
                            ';
        $data['equipment'] = $this->Contract->contractEquipment(['contract_id'=>$contract_id,'state'=>1,'rq_contract_equipment.is_delete'=>0],$Equipment_join,$Equipment_field);
        $newArr = [];
        foreach ($data['equipment'] as $key => $value){
            $data['equipment'][$key]['Remark'] = $this->Contract->equipmentsRemark(['contract_equipment_id'=>$value['id']]);
            for($i=1;$i<=8;$i++){
                if ($i == 1 || $i == 6){
                    continue;
                }
                $record = $this->Contract->equipmentRecord($i,['equipment_id'=>$value['id']]);
                if ($record){
                    foreach ($record as $rk => $rv){
                        switch ($i){
                            case 1:
                                $type = "新装";
                                break;
                            case 2:
                                $type = "维修";
                                break;
                            case 3:
                                $type = "换芯";
                                break;
                            case 4:
                                $type = "拆机";
                                break;
                            case 5:
                                $type = "移机";
                                break;
                            case 6:
                                $type = "续费";
                                break;
                            case 7:
                                $type = "换芯+维修";
                                break;
                            case 8:
                                $type = "续费+维修";
                                break;
                            default:
                                $type = "未知类型";
                        }
                        $record[$rk]['type'] = $type;
                        $data['equipment'][$key]['record'][] = $record[$rk];
                    }
                }
            }
        }
//        dump($data);
        //合同操作记录
        $ContractLogWhere['contract_id'] = $contract_id;
        $ContractLog = $this->Contract->getContractLog($ContractLogWhere,[],'');
        foreach ($ContractLog as $clk=>$clv){
            $ContractLog[$clk]['do_time'] = date('Y-m-d H:i:s',$clv['do_time']);
            switch ($clv['do_type']){
                case 1:
                    $ContractLog[$clk]['do_type'] = "新增";
                    break;
                case 2:
                    $ContractLog[$clk]['do_type'] = "删除";
                    break;
                case 3:
                    $ContractLog[$clk]['do_type'] = "修改";
                    break;
                case 4:
                    $ContractLog[$clk]['do_type'] = "解绑";
                    break;
            }
        }
        foreach ($ContractLog as $key => $value){
            switch ($value['terminal_type']){
                //总后台
                case 0:
                    if ($value['do_id'] == 0){
                        $ContractLog[$key]['name'] = '系统';
                        $ContractLog[$key]['title'] = '系统';
                        break;
                    }
                    $findUserJoin = [
                        ['rq_admin_auth_group','rq_admin_user.groupid = rq_admin_auth_group.id']
                    ];
                    $findUserWhere = [
                        'rq_admin_user.id'=>$value['do_id']
                    ];
                    $findUserField = 'rq_admin_user.name,rq_admin_auth_group.title';
                    $admin_user = $this->Contract->findUser('admin_user',$findUserWhere,$findUserJoin,$findUserField);
                    $ContractLog[$key]['name'] = $admin_user['name'];
                    $ContractLog[$key]['title'] = $admin_user['title'];
                    break;
                //运营中心
                case 1:
                    $findUserJoin = [
                        ['rq_operation_auth_group','rq_operation_user.groupid = rq_operation_auth_group.id']
                    ];
                    $findUserWhere = [
                        'rq_operation_user.id'=>$value['do_id']
                    ];
                    $findUserField = 'rq_operation_user.name,rq_operation_auth_group.title';
                    $admin_user = $this->Contract->findUser('operation_user',$findUserWhere,$findUserJoin,$findUserField);
                    $ContractLog[$key]['name'] = $admin_user['name'];
                    $ContractLog[$key]['title'] = $admin_user['title'];
                    break;
                //行政区域
                case 2:
                    $findUserJoin = [
                        ['rq_administrative_auth_group','rq_administrative_user.groupid = rq_administrative_auth_group.id']
                    ];
                    $findUserWhere = [
                        'rq_administrative_user.id'=>$value['do_id']
                    ];
                    $findUserField = 'rq_administrative_user.name,rq_administrative_auth_group.title';
                    $admin_user = $this->Contract->findUser('administrative_user',$findUserWhere,$findUserJoin,$findUserField);
                    $ContractLog[$key]['name'] = $admin_user['name'];
                    $ContractLog[$key]['title'] = $admin_user['title'];
                    break;
            }
        }
        //获取合同原始配件
        $PartsObj = new Parts();
        $PartsWhere = [
            'contract_id'=>$contract_id,
            'is_original'=>1,
            'rq_equipments_parts.is_delete'=>0
        ];
        $PartsJoin = [
            ['rq_parts','rq_equipments_parts.parts_id = rq_parts.parts_id']
        ];
        $PartsField = 'parts_name,parts_number';
        $Parts = $PartsObj->getContractParts($PartsWhere,$PartsField,$PartsJoin);
        //追加原始配件
        $data['original_parts'] = $Parts;
        $data['ContractLog'] = $ContractLog;
        $this->assign('data',$data);
        //查询设备
        $equipments = new Equipments();
        $equipmentsInfo = $equipments->getList();
        $this->view->equipmentsinfo = json_encode($equipmentsInfo);
        //渲染模板
        return $this->fetch("contract/details");

    }

    /*
     *  获取设备操作记录
     *  @author 李港
     *  @param int  $id                     合同设备关联ID
     *  @param int  $equipments_number      设备编号
     *  @param int  $page                   页数
     *  @return array|json
     */
    public function getEquipmentRecord()
    {
        //获取合同设备关联ID
        $id = input('?get.id')?input('get.id'):false;
        $equipments_number = input('?get.equipments_number')?input('get.equipments_number'):false;
        $page = input('?get.page')?input('get.page'):1;
        if (!$id || !$equipments_number){
            return ['code'=>0,'msg'=>'缺少必要参数'];
        }
        $recordData = [];
        for($i=1;$i<=8;$i++){
            $record = $this->Contract->equipmentRecord($i,['equipment_id'=>$id]);
            if ($record){
                foreach ($record as $rk => $rv){
                    switch ($i){
                        case 1:
                            $type = "新装";
                            break;
                        case 2:
                            $type = "维修";
                            break;
                        case 3:
                            $type = "换芯";
                            break;
                        case 4:
                            $type = "拆机";
                            break;
                        case 5:
                            $type = "移机";
                            break;
                        case 6:
                            $type = "续费";
                            break;
                        case 7:
                            $type = "换芯+维修";
                            break;
                        case 8:
                            $type = "续费+维修";
                            break;
                        default:
                            $type = "未知类型";
                    }
                    $record[$rk]['type'] = $type;
                    $recordData[] = $record[$rk];
                }
            }
        }
        $Engineer = new Engineers();
        $ContractEquipment = new ContractEquipment();
        $EngineerField = 'engineers_name';
        $EquipmentsField = 'equipments_address';
        foreach ($recordData as $key => $value){
            $recordData[$key]['equipments_number'] = $equipments_number;
            //操作人
            if ($value['repair_id'] == 0){
                $recordData[$key]['repair_id'] = '系统';
            }else{
                $recordData[$key]['repair_id'] = $Engineer->getEngineerInfo(['engineers_id'=>$value['repair_id']],$EngineerField)['engineers_name'];
            }
            //设备地址
            $recordData[$key]['address'] = $ContractEquipment->findEquipmentsData(['id'=>$id],[],$EquipmentsField)['equipments_address'];
            //操作时间
            foreach (array_keys($recordData[$key]) as $k=>$v){
                if (strpos($v,'time')){
                    $recordData[$key]['do_date'] = date('Y-m-d H:i:s',$value[$v]);
                    unset($recordData[$key][$v]);
                    continue;
                }
            }
        }
        $Configuration = new Configuration();
        //每页显示数目
        $listRows = intval($Configuration->getkey('pagesize'));
        //总条数
        $recordTotal = count($recordData);
        //总页数
        $recordTotalPage = ceil($recordTotal / $listRows);
        if ($page == 1){
            $start = 0;
        }else{
            $start = ($page - 1) * $listRows;
        }
        $data = array_slice($recordData,$start,$listRows);
        $data['data'] = $data;
        $data['page']['currentPage'] = $page;
        $data['page']['lastPage'] = $recordTotalPage;
        $data['page']['count'] = $recordTotal;
        if (input('?get.page')){
            return json($data);
        }
    }

    /*
     *  获取合同操作记录
     */
    public function getAllContractLog()
    {
        $contract_id = input('?get.contract_id')?input('get.contract_id/d'):false;
        if (!$contract_id){
            return ['code'=>0,'msg'=>'缺少必要参数'];
        }
        $ContractLogWhere['contract_id'] = $contract_id;
        $page =1;
        $page_number = '';
        $ContractLog = $this->Contract->getContractLog($ContractLogWhere,[],'',$page,$page_number);
        foreach ($ContractLog as $clk=>$clv){
            $ContractLog[$clk]['do_time'] = date('Y-m-d H:i:s',$clv['do_time']);
            switch ($clv['do_type']){
                case 1:
                    $ContractLog[$clk]['do_type'] = "新增";
                    break;
                case 2:
                    $ContractLog[$clk]['do_type'] = "删除";
                    break;
                case 3:
                    $ContractLog[$clk]['do_type'] = "修改";
                    break;
                case 4:
                    $ContractLog[$clk]['do_type'] = "解绑";
                    break;
            }
        }
        foreach ($ContractLog as $key => $value){
            switch ($value['terminal_type']){
                //总后台
                case 0:
                    if ($value['do_id'] == 0){
                        $ContractLog[$key]['name'] = '系统';
                        $ContractLog[$key]['title'] = '系统';
                        break;
                    }
                    $findUserJoin = [
                        ['rq_admin_auth_group','rq_admin_user.groupid = rq_admin_auth_group.id']
                    ];
                    $findUserWhere = [
                        'rq_admin_user.id'=>$value['do_id']
                    ];
                    $findUserField = 'rq_admin_user.name,rq_admin_auth_group.title';
                    $admin_user = $this->Contract->findUser('admin_user',$findUserWhere,$findUserJoin,$findUserField);
                    $ContractLog[$key]['name'] = $admin_user['name'];
                    $ContractLog[$key]['title'] = $admin_user['title'];
                    break;
                //运营中心
                case 1:
                    $findUserJoin = [
                        ['rq_operation_auth_group','rq_operation_user.groupid = rq_operation_auth_group.id']
                    ];
                    $findUserWhere = [
                        'rq_operation_user.id'=>$value['do_id']
                    ];
                    $findUserField = 'rq_operation_user.name,rq_operation_auth_group.title';
                    $admin_user = $this->Contract->findUser('operation_user',$findUserWhere,$findUserJoin,$findUserField);
                    $ContractLog[$key]['name'] = $admin_user['name'];
                    $ContractLog[$key]['title'] = $admin_user['title'];
                    break;
                //行政区域
                case 2:
                    $findUserJoin = [
                        ['rq_administrative_auth_group','rq_administrative_user.groupid = rq_administrative_auth_group.id']
                    ];
                    $findUserWhere = [
                        'rq_administrative_user.id'=>$value['do_id']
                    ];
                    $findUserField = 'rq_administrative_user.name,rq_administrative_auth_group.title';
                    $admin_user = $this->Contract->findUser('administrative_user',$findUserWhere,$findUserJoin,$findUserField);
                    $ContractLog[$key]['name'] = $admin_user['name'];
                    $ContractLog[$key]['title'] = $admin_user['title'];
                    break;
            }
        }
        $data['code'] = 1;
        $data['data'] = $ContractLog;
        return json($data);
    }

    /*
     * 添加设备备注
     * @author ligang
     * @Request equipments_id             int    GET   设备ID
     */
    public function addRemark()
    {
        $id = input('?get.id')?input('get.id'):false;
        if (!$id){
            return ['code'=>0,'msg'=>'缺少必要参数'];
        }
        $contract_id = input('?get.contract_id')?input('get.contract_id'):false;
        if (!$contract_id){
            return ['code'=>0,'msg'=>'缺少必要参数'];
        }
        $equipments_id = input('?get.equipments_id')?input('get.equipments_id/d'):false;
        if (!$equipments_id){
            return ['code'=>0,'msg'=>'缺少必要参数'];
        }
        $remark = input('?get.remark')?input('get.remark'):false;
        if (!$remark){
            return ['code'=>0,'msg'=>'请输入备注信息'];
        }
        //组装数据
        $data['contract_equipment_id'] = $id;
        $data['equipments_remark'] = trim($remark);
        $Equipments_add = $this->Contract->addEquipmentsRemark($data);
        //添加合同操作记录
        $where['id'] = $id;
        $Equipment = $this->Contract->contractEquipment($where,[],'equipments_number')[0];
        $Log = [
            'contract_id'=>$contract_id,
            'do_type'=>1,
            'terminal_type'=>0,
            'new_value'=>$Equipment['equipments_number']."设备备注：".$remark,
        ];
        if ($Equipments_add){
            $this->Contract->addContractLog($Log);
            return ['code'=>1,'msg'=>'添加成功'];
        }else{
            return ['code'=>0,'msg'=>'添加失败'];
        }
    }

    /*
         * 解绑设备
         * @author ligang
         * @Request equipments_id             int    GET   设备ID
         */
    public function unBindEquipments()
    {
        $id = input('get.id');
        $ContractEquipmentObj = new ContractEquipment();
        $EquipmentsInfo = $ContractEquipmentObj->getEquipmentsData(['id'=>$id])[0];
        if ($EquipmentsInfo){
            $result = $ContractEquipmentObj->edit(['id'=>$id],['state'=>0]);
            if ($result){
                $Log = [
                    'contract_id'=>$EquipmentsInfo['contract_id'],
                    'do_type'=>4,
                    'terminal_type'=>2,
                    'old_value'=>'设备编号：'.$EquipmentsInfo['equipments_number'],
                    'new_value'=>''
                ];
                $this->Contract->addContractLog($Log);


                //判断合同下是否存在设备
                $ContractEquipmentDataWhere = [
                    'contract_id'=>$EquipmentsInfo['contract_id'],
                    'state'=>1,
                    'is_delete'=>0,
                ];
                $ContractEquipmentData = $ContractEquipmentObj->getEquipmentsData($ContractEquipmentDataWhere);
                if (empty($ContractEquipmentData)){
                    $Contract = new Contract();
                    $ContractWhere = [
                        'contract_id'=>$EquipmentsInfo['contract_id']
                    ];
                    $ContractData = [
                        'contract_status'=>7,
                        'update_time'=>time()
                    ];
                    $Contract->updateContract($ContractWhere,$ContractData);
                    $ContractLog = [
                        'contract_id'=>$EquipmentsInfo['contract_id'],
                        'do_type'=>3,
                        'terminal_type'=>2,
                        'old_value'=>'',
                        'new_value'=>'合同下不存在设备，改为无效合同'
                    ];
                    $this->Contract->addContractLog($ContractLog);
                }
                return ['code'=>1,'msg'=>'解绑成功'];
            }else{
                return ['code'=>0,'msg'=>'解绑失败'];
            }
        }else{
            return ['code'=>0,'msg'=>'设备不存在'];
        }

    }

    /*
     * 修改设备
     * @author ligang
     * @Request equipments_id             int    GET   设备ID
     */
    public function editEquipments()
    {
        $contract_id = input('?get.contract_id')?input('get.contract_id'):false;
        $id = input('?get.id')?input('get.id'):false;
        $equipments_id = input('?get.equipments_id')?input('get.equipments_id/d'):false;
        $equipments_name = input('?get.equipments_name')?trim(input('get.equipments_name')):false;
        $equipments_number = !empty(input('param.equipments_number'))?trim(input('param.equipments_number')):false;
        $equipmentsObj = new ContractEquipment();
        $equipmentsInfo = $equipmentsObj->getOne(['id'=>$equipments_id]);
        if(!$equipmentsInfo){
            return ['code'=>0,'msg'=>'非法操作'];
        }

        if (!$equipments_id || !$contract_id || !$id){
            return ['code'=>0,'msg'=>'缺少必要参数'];
        }
        if (!$equipments_name){
            return ['code'=>0,'msg'=>'请输入设备型号'];
        }
        if (!$equipments_number){
            return ['code'=>0,'msg'=>'请输入设备编号'];
        }
        if (strlen($equipments_number) != 10){
            return ['code'=>0,'msg'=>'设备编号为10位纯数字'];
        }


        $ContractEquipmentObj = new ContractEquipment();
        $EquipmentObj = new Equipments();
        $checkNumber = $ContractEquipmentObj->getEquipmentsData(['equipments_number'=>$equipments_number]);
        if ($checkNumber){
            return ['code'=>0,'msg'=>'设备编号已存在'];
        }
        $where['id'] = $id;
        $join = [
            ['rq_equipments','rq_contract_equipment.equipments_id = rq_equipments.equipments_id']
        ];
        $equipments = $ContractEquipmentObj->getEquipmentsData($where,$join,'equipments_name,rq_contract_equipment.equipments_number')[0];
        $newWhere = [
            'equipments_id'=>$equipments_name
        ];
        $newEquipmentsName = $EquipmentObj->selectOne($newWhere);
        $updateData['id'] = $id;
        $updateData['equipments_id'] = $equipments_name;
        $updateData['equipments_number'] = $equipments_number;
        $updateData['uptime'] = time();
        $result = $ContractEquipmentObj->updateEquipmentsData([],$updateData);
        if ($result){
            $data['old_value'] = '设备ID:'.$equipments['equipments_name'].';'.'设备编号:'.$equipments['equipments_number'];
            $data['new_value'] = '设备ID:'.$newEquipmentsName['equipments_name'].';'.'设备编号:'.$updateData['equipments_number'];
            $data['contract_id'] = $contract_id;
            $data['do_type'] = 3;
            $data['terminal_type'] = 2;
            $this->Contract->addContractLog($data);
            return ['code'=>1,'msg'=>'修改成功'];
        }else{
            return ['code'=>0,'msg'=>'修改失败'];
        }


    }
    /*
     *  合同图片
     *  @author ligang
     *  @Request contract_id             int    GET   合同ID
     *  @return array
     */
    public function contractImages()
    {
        $id = input('?get.id')?input('get.id'):false;
        if (!$id){
            return ['code'=>0,'msg'=>'非法请求'];
        }
        $data = $this->Contract->contractImg(['contract_id'=>$id],'contract_imgs');
        if (is_null(json_decode($data['contract_imgs']))){
            return ['code'=>0,'msg'=>'数据格式错误'];
        }
        $imgUrl = json_decode($data['contract_imgs']);
        $array = [
            'code'=>1,
            'data'=>$imgUrl
        ];
        return  json($array);
    }

    /**
     * 删除合同     逻辑删除
     * @author ligang
     * @Request contract_id             int    GET   合同ID
     * @return array
     */
    public function delete()
    {
        //检验get参数
        if (input('?get.data')){
            //获取参数并强制转换为int型
            $contract_id = input('')['data'];
        }else{
            return json(array('code'=>0,'msg'=>'缺少必要参数'));
        }

        foreach ($contract_id as $key => $value){
            if ($value == 'on'){
                continue;
            }

            $checkData = $this->Contract->selectOne(array('contract_id'=>intval($value)));

            if (empty($checkData)){
                return json(array('code'=>0,'msg'=>"删除失败"));
            }
            $res = $this->Contract->deleteOne($value);
            if($res === false){
                return json(array('code'=>0,'msg'=>'删除失败'));
            }
        }
        return json(array('code'=>1,'msg'=>'删除成功'));
    }

    /**
     * 根据运营中心获取行政中心
     * @author ligang
     * @Request operation_id             int    GET   合同ID
     * @return array
     */
    public function selectAdministrative()
    {
        $where = array();
        if(input('get.operation/d')){
            $where['operation'] = input('get.operation/d');
        }
        $administrative = $this->Contract->selectAdministrativeInfo($where);
        return $administrative;
    }



}
